package com.example.transaction.controller;

import com.example.transaction.model.Account;
import com.example.transaction.service.AccountService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/accounts")
@RequiredArgsConstructor
@Slf4j
public class AccountController {

    private final AccountService accountService;

    @PostMapping
    public ResponseEntity<Account> createAccount(@RequestBody Map<String, Object> request) {
        String accountNumber = (String) request.get("accountNumber");
        String ownerName = (String) request.get("ownerName");
        BigDecimal initialBalance = new BigDecimal(request.get("initialBalance").toString());
        
        Account account = accountService.createAccount(accountNumber, ownerName, initialBalance);
        return ResponseEntity.ok(account);
    }

    @GetMapping("/{accountNumber}")
    public ResponseEntity<Account> getAccount(@PathVariable String accountNumber) {
        Account account = accountService.getAccount(accountNumber);
        return ResponseEntity.ok(account);
    }

    @GetMapping
    public ResponseEntity<List<Account>> getAllAccounts() {
        List<Account> accounts = accountService.getAllAccounts();
        return ResponseEntity.ok(accounts);
    }

    @PostMapping("/{accountNumber}/deposit")
    public ResponseEntity<Account> deposit(
            @PathVariable String accountNumber,
            @RequestBody Map<String, Object> request) {
        BigDecimal amount = new BigDecimal(request.get("amount").toString());
        Account account = accountService.deposit(accountNumber, amount);
        return ResponseEntity.ok(account);
    }

    @PostMapping("/{accountNumber}/withdraw")
    public ResponseEntity<Account> withdraw(
            @PathVariable String accountNumber,
            @RequestBody Map<String, Object> request) {
        BigDecimal amount = new BigDecimal(request.get("amount").toString());
        Account account = accountService.withdraw(accountNumber, amount);
        return ResponseEntity.ok(account);
    }

    @PostMapping("/transfer")
    public ResponseEntity<String> transfer(@RequestBody Map<String, Object> request) {
        String fromAccountNumber = (String) request.get("fromAccountNumber");
        String toAccountNumber = (String) request.get("toAccountNumber");
        BigDecimal amount = new BigDecimal(request.get("amount").toString());
        
        try {
            accountService.transfer(fromAccountNumber, toAccountNumber, amount);
            return ResponseEntity.ok("Transfer completed successfully");
        } catch (Exception e) {
            log.error("Transfer failed: {}", e.getMessage());
            return ResponseEntity.badRequest().body("Transfer failed: " + e.getMessage());
        }
    }

    @PostMapping("/batch-transfer")
    public ResponseEntity<String> batchTransfer(@RequestBody List<Map<String, Object>> requests) {
        List<AccountService.TransferRequest> transfers = requests.stream()
                .map(req -> new AccountService.TransferRequest(
                        (String) req.get("fromAccountNumber"),
                        (String) req.get("toAccountNumber"),
                        new BigDecimal(req.get("amount").toString())
                ))
                .toList();
        
        try {
            accountService.batchTransfer(transfers);
            return ResponseEntity.ok("Batch transfer completed");
        } catch (Exception e) {
            log.error("Batch transfer failed: {}", e.getMessage());
            return ResponseEntity.badRequest().body("Batch transfer failed: " + e.getMessage());
        }
    }
}